package com.tg.app.bean;

import android.text.TextUtils;

import com.tg.app.camera.AVIOCTRLDEFs;
import com.tg.app.camera.Camera;
import com.tg.appcommon.android.Packet;
import com.tg.appcommon.android.TGLog;

import java.io.UnsupportedEncodingException;

import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;
import io.objectbox.annotation.Transient;

@Entity
public class DevicePresetPoints {
    @Id
    public long id;//设备ID
    public String uuid;//设备uuid
    public int flags;
    public int num;
    public String name;
    public float x;
    public float y;
    public float z;
    @Transient
    public AVIOCTRLDEFs.Tcis_SetPtzPosReq pos;

    public byte[] parseDeleteContent(int posType) {
        return parseContent(AVIOCTRLDEFs.PSP_F_DISABLED, posType);
    }


    public String getFileName(Camera camera) {
        if (camera == null) {
            return "";
        }
        if (pos == null) {
            pos = new AVIOCTRLDEFs.Tcis_SetPtzPosReq(x, y, z);
        }
        return String.format("%s_%d_%f_%f_%f", camera.uid, num, pos.x, pos.y, pos.z);
    }

    public byte[] parseContent(int t, int posType) {
        byte[] b = null;
        if (!TextUtils.isEmpty(name)) {
            try {
                b = name.getBytes("utf-8");

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        TGLog.d("PrePositionAdapter", "TCI_CMD_SET_COMMAND_RESP pos = ", pos);
        if (posType == AVIOCTRLDEFs.PSP_BY_POS) {
            TGLog.d("PrePositionAdapter", "TCI_CMD_SET_COMMAND_RESP pos111 = ", pos);
            byte[] result = new byte[48];
            byte[] f = Packet.shortToByteArray_Little((short) t);
            System.arraycopy(f, 0, result, 0, f.length);
            byte[] n = Packet.shortToByteArray_Little((short)num);
            System.arraycopy(n, 0, result, 2, n.length);
            byte[] p = AVIOCTRLDEFs.Tcis_SetPtzPosReq.parseContent(pos.x, pos.y, pos.z);
            System.arraycopy(p, 0, result, 4, p.length);

            if (b != null) {
                int nameLen = b.length;
                if (nameLen > 32) {
                    nameLen = 32;
                }
                System.arraycopy(b, 0, result, 16, nameLen);
            }
            return result;
        }

        if (posType == AVIOCTRLDEFs.PSP_BY_NO) {
            byte[] result = new byte[36];
            byte[] f = Packet.shortToByteArray_Little((short) t);
            byte[] n = Packet.shortToByteArray_Little((short)num);
            System.arraycopy(f, 0, result, 0, f.length);
            System.arraycopy(n, 0, result, 2, n.length);
            if (b != null) {
                System.arraycopy(b, 0, result, 4, b.length);
            }
            return result;
        }


        if (posType == AVIOCTRLDEFs.PSP_BY_NO_NONAME) {
            byte[] result = new byte[4];
            byte[] f = Packet.shortToByteArray_Little((short) t);
            byte[] n = Packet.shortToByteArray_Little((short) num);
            System.arraycopy(f, 0, result, 0, f.length);
            System.arraycopy(n, 0, result, 2, n.length);
            return result;
        }

        byte[] result = new byte[16];
        byte[] f = Packet.shortToByteArray_Little((short) t);
        byte[] n = Packet.shortToByteArray_Little((short) num);
        System.arraycopy(f, 0, result, 0, f.length);
        System.arraycopy(n, 0, result, 2, n.length);

        byte[] p = AVIOCTRLDEFs.Tcis_SetPtzPosReq.parseContent(pos.x, pos.y, pos.z);
        System.arraycopy(p, 0, result, 4, p.length);
        return result;
    }


    public byte[] parseContentEx(int posType) {
        byte[] b = null;
        if (!TextUtils.isEmpty(name)) {
            try {
                b = name.getBytes("utf-8");

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }


        byte[] result = new byte[0];
        if (posType == AVIOCTRLDEFs.PSP_BY_POS) {
            result = new byte[48];
            byte[] f = Packet.shortToByteArray_Little((short) flags);
            byte[] n = Packet.shortToByteArray_Little((short) num);
            System.arraycopy(f, 0, result, 0, f.length);
            System.arraycopy(n, 0, result, 2, n.length);
            if (pos == null){
                pos = new AVIOCTRLDEFs.Tcis_SetPtzPosReq(0, 0, 0);
            }
            byte[] p = AVIOCTRLDEFs.Tcis_SetPtzPosReq.parseContent(pos.x, pos.y, pos.z);
            System.arraycopy(p, 0, result, 4, p.length);

            if (b != null) {
                int nameLen = b.length;
                if (nameLen > 32) {
                    nameLen = 32;
                }
                System.arraycopy(b, 0, result, 16, nameLen);
            }

        } else if (posType == AVIOCTRLDEFs.PSP_BY_NO) {
            result = new byte[36];
            byte[] f = Packet.shortToByteArray_Little((short) flags);
            byte[] n = Packet.shortToByteArray_Little((short) num);
            System.arraycopy(f, 0, result, 0, f.length);
            System.arraycopy(n, 0, result, 2, n.length);
            if (b != null) {
                int nameLen = b.length;
                if (nameLen > 32) {
                    nameLen = 32;
                }
                System.arraycopy(b, 0, result, 4, nameLen);
            }

        } else if (posType == AVIOCTRLDEFs.PSP_BY_NO_NONAME){
            result = new byte[4];
            byte[] f = Packet.shortToByteArray_Little((short) flags);
            byte[] n = Packet.shortToByteArray_Little((short) num);
            System.arraycopy(f, 0, result, 0, f.length);
            System.arraycopy(n, 0, result, 2, n.length);

        }else if (posType == AVIOCTRLDEFs.PSP_BY_POS_NONAME){
            result = new byte[16];
            byte[] f = Packet.shortToByteArray_Little((short) flags);
            byte[] n = Packet.shortToByteArray_Little((short) num);
            System.arraycopy(f, 0, result, 0, f.length);
            System.arraycopy(n, 0, result, 2, n.length);

            byte[] p = AVIOCTRLDEFs.Tcis_SetPtzPosReq.parseContent(pos.x, pos.y, pos.z);
            System.arraycopy(p, 0, result, 4, p.length);
        }
        return result;


    }
}
